﻿using SX.Web.Framework.Permissions;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using SX.Utils;
using System.Security.Claims;

namespace SX.Web.Framework.Html
{
    public static class HtmlHelperExtensions
    {
        private static List<string> default_button_list = new List<string> {"add","edit","del", "cancel", "save", "refresh", "import", "export" };
        /// <summary>
        /// 工具栏按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public static IHtmlContent ToolbarCustom(this IHtmlHelper htmlHelper, string permissionName)
        {
            var service = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IPermissionService>();
            string userId = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("userid");// 用户id
            var sup = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("is_sup"); // 是否是超级管理员
            List<dynamic> buttonList = service.GetButtonList(CommonHelper.ToInt64(userId), permissionName, sup == "1");
            var content = new StringContent(); 
            foreach (var item in buttonList)
            {
                string permission = item.permission.ToString();
                string btnName = permission.Substring(permission.LastIndexOf('.') + 1).ToLower();
                if (!default_button_list.Contains(btnName))
                {
                    content.AppendFormat("<a id=\"a_btn{1}\"  plain=\"true\" class=\"mini-button\" iconCls=\"{2}\" title=\"{0}\" onClick =\"{3}\">{0}</a>",
                   item.name, item.menu_id, item.icon, item.js_method);
                } 
            }
            return content;
        }
        /// <summary>
        /// 工具栏按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public static IHtmlContent ToolbarItems(this IHtmlHelper htmlHelper, string permissionName)
        {
            var service = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IPermissionService>();
            string userId = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("userid");// 用户id
            var sup = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("is_sup"); // 是否是超级管理员
            List<dynamic> buttonList = service.GetButtonList(CommonHelper.ToInt64(userId), permissionName, sup == "1");
            List<string> buttons = new List<string>() { "refresh"};
            foreach (var item in buttonList)
            {
                string permission = item.permission.ToString();
                string btnName = permission.Substring(permission.LastIndexOf('.')+1).ToLower();
                if (default_button_list.Contains(btnName))
                {
                    buttons.Add(btnName);
                } 
            }
            return new StringContent( buttons.Join(","));
        }
        /// <summary>
        /// 工具栏按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public static IHtmlContent ToolbarButton(this IHtmlHelper htmlHelper,string permissionName) {
            var service = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IPermissionService>();
            string userId = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("userid");// 用户id
            var sup = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("is_sup"); // 是否是超级管理员
            List<dynamic> buttonList = service.GetButtonList(CommonHelper.ToInt64(userId), permissionName, sup == "1");
            var content = new StringContent();
            foreach (var item in buttonList)
            {
                content.AppendFormat("<a id=\"a_btn{1}\"  plain=\"true\" class=\"mini-button\" iconCls=\"{2}\" title=\"{0}\" onClick =\"{3}\">{0}</a>", 
                    item.name, item.menu_id, item.icon, item.js_method);
            }
            return content;
        }
        /// <summary>
        /// 根据权限渲染按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public static IHtmlContent PermissionButton(this IHtmlHelper htmlHelper, string permissionName)
        {
            var service = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IPermissionService>();
            string userId = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("userid");// 用户id
            var sup = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("is_sup"); // 是否是超级管理员
            dynamic button = service.GetButton(CommonHelper.ToInt64(userId), permissionName, sup == "1");
            var content = new StringContent();
            if (button != null)
            {
                content.AppendFormat("<a id=\"a_btn{1}\"  plain=\"true\" class=\"mini-button\" iconCls=\"{2}\" title=\"{0}\" onClick =\"{3}\">{0}</a>",
                    button.name, button.menu_id, button.icon, button.js_method); 
            } 
            return content;
        }

        /// <summary>
        /// 检查当前用户是否有某个权限
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="permissionName">权限名</param>
        /// <returns></returns>
        public static bool CheckPermission(this IHtmlHelper htmlHelper, string permissionName) {

            var service = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IPermissionService>();
            string userId = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("userid");// 用户id
            var sup = htmlHelper.ViewContext.HttpContext.User.FindFirstValue("is_sup"); // 是否是超级管理员
            if (sup == "1") return true; 
            return service.CheckPermission(CommonHelper.ToInt64(userId), permissionName);  
        }
        /// <summary>
        /// css site (默认值空)
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <returns></returns>
        public static string GetCssSiteUrl(this IHtmlHelper htmlHelper) {
            return ConfigHelper.GetApp("SiteSetting:CssSite");
        }
        /// <summary>
        /// js site(默认值空)
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <returns></returns>
        public static string GetJsSiteUrl(this IHtmlHelper htmlHelper)
        {
            return ConfigHelper.GetApp("SiteSetting:JsSite");
        }
    }
}
